package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.UserAuthenticationDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.UserAuthenticationMapper;
import  java.util.List;

import com.example.makerspace.vo.PersonVO;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class UserAuthenticationDAO  extends BaseDAO<UserAuthenticationDO>{
    @Resource
    private UserAuthenticationMapper userAuthenticationMapper;
    @PostConstruct
    public void init(){
        this.mapper = userAuthenticationMapper;
    }

    public List<UserAuthenticationDO> selectByPhoneNumber(String username) {
        return  mapper.selectList(new QueryWrapper<UserAuthenticationDO>().eq("phone",username));
    }

    public void deleteByUsername(String username) {
        mapper.delete(new QueryWrapper<UserAuthenticationDO>().eq("phone",username));
    }

    public void updateByUserName(UserAuthenticationDO userAuthenticationDO) {
        UpdateWrapper<UserAuthenticationDO> wrapper = new UpdateWrapper<UserAuthenticationDO>();
        wrapper.eq("phone",userAuthenticationDO.getPhone());
        mapper.update(userAuthenticationDO,wrapper);
    }

    public IPage<UserAuthenticationDO> selectByName(Pagination pagination, String individualName) {
        QueryWrapper<UserAuthenticationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.like("name",individualName).or().like("phone",individualName);
        return mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }


    public IPage<PersonVO> selectByOtherType(Page<PersonVO> page) {
          return  userAuthenticationMapper.selectByOtherType(page);
    }

    public IPage<PersonVO> selectByOtherName(Page<PersonVO> page, String name) {
        return  userAuthenticationMapper.selectByOtherName(page,name);
    }
}
